var enterprise_swap_temp=db.enterprise_swap.find({"analyse_date" : ISODate("2017-09-11T16:00:00Z")},{analyse_date:1,area_id:1,location_exception_rate:1,track_complete_rate:1,data_correct_rate:1,vehicle_online_rate:1,avg_vehicle_tired_duration:1,avg_vehicle_speed_counts:1,vehicle_access_rate:1,reply_rate:1}).sort({area_id:1});


while(enterprise_swap_temp.hasNext()){

    var enterprise_temp = enterprise_swap_temp.next();
    var area_swap_temp=db.area_swap.find({"analyse_date" : enterprise_temp.analyse_date,area_id:enterprise_temp.area_id},{area_id:1,avg_vehicle_tired_duration:1,avg_vehicle_speed_counts:1}).sort({area_id:1});
    while(area_swap_temp.hasNext()){

        var area_temp = area_swap_temp.next();
        if(enterprise_temp.area_id==area_temp.area_id){

            var _id = enterprise_temp._id;  
            var score_temp=NumberInt(0);
            var avg_speed_score=NumberInt(0);
            var avg_tired_score=NumberInt(0);

            if(enterprise_temp.location_exception_rate<=5){
				score_temp+=enterprise_temp.location_exception_rate*0.1;
			}
	
			if(enterprise_temp.track_complete_rate>=70){
				score_temp+=enterprise_temp.track_complete_rate*0.15;
			}
	
            if(enterprise_temp.data_correct_rate>=80){
				score_temp+=enterprise_temp.data_correct_rate*0.15;
			}  
				
            if(enterprise_temp.vehicle_online_rate>=90){
				score_temp+=enterprise_temp.vehicle_online_rate*0.05;
			}   
	
			score_temp+=enterprise_temp.vehicle_access_rate*0.05;
			score_temp+=enterprise_temp.reply_rate*0.1;

            if(enterprise_temp.avg_vehicle_tired_duration<(2*area_temp.avg_vehicle_tired_duration)){
                if(enterprise_temp.avg_vehicle_tired_duration<=(area_temp.avg_vehicle_tired_duration)){
                    avg_tired_score=10+((area_temp.avg_vehicle_tired_duration-enterprise_temp.avg_vehicle_tired_duration)/area_temp.avg_vehicle_tired_duration)*10;
                }else{
                    avg_tired_score=(((2*area_temp.avg_vehicle_tired_duration)-enterprise_temp.avg_vehicle_tired_duration)/area_temp.avg_vehicle_tired_duration)*10;
                }
				score_temp+=avg_tired_score;
            }   

			if(enterprise_temp.avg_vehicle_speed_counts<(2*area_temp.avg_vehicle_speed_counts)){                    
                if(enterprise_temp.avg_vehicle_speed_counts<=(area_temp.avg_vehicle_speed_counts)){
                    avg_speed_score=10+((area_temp.avg_vehicle_speed_counts-enterprise_temp.avg_vehicle_speed_counts)/area_temp.avg_vehicle_speed_counts)*10;
                }else{
                    avg_speed_score=(((2*area_temp.avg_vehicle_speed_counts)-enterprise_temp.avg_vehicle_speed_counts)/area_temp.avg_vehicle_speed_counts)*10;
                }                    
                score_temp+=avg_speed_score;	 
            }

			if(score_temp>0){
				var obj = {score:NumberInt(Math.round(score_temp))};
				db.enterprise_swap.update({_id:_id},{$set:obj},false);
			}	
        }  
    }
};  
